package com.junzhiit.nengji.cms.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.junzhiit.nengji.bo.MemberCardUseBO;
import com.junzhiit.nengji.cms.service.MemberCardUseService;
import com.junzhiit.nengji.cms.web.exception.ShopNotFoundException;
import com.junzhiit.nengji.cms.web.util.ViewObjectUtil;
import com.junzhiit.nengji.cms.web.vo.DataTablesParamVO;
import com.junzhiit.nengji.cms.web.vo.MemberCardUseVO;
import com.junzhiit.nengji.cms.web.vo.SystemUserSessionVO;
import com.junzhiit.nengji.dao.MemberCardUseDAO;
import com.junzhiit.nengji.dao.ShopDAO;
import com.junzhiit.nengji.dao.cache.SystemUserCacheDAO;
import com.junzhiit.nengji.dao.util.DIYKey;
import com.junzhiit.nengji.model.Shop;

@Service
public class MemberCardUseServiceImpl extends BaseServiceImpl implements MemberCardUseService {
	
	@Autowired
	private SystemUserCacheDAO systemUserCacheDAO;

	@Autowired
	private ShopDAO shopDAO;
	
	@Autowired
	private MemberCardUseDAO memberCardUseDAO;
	
	@Override
	public Map<String, Object> findAll(MemberCardUseVO vo, DataTablesParamVO dtp, String sessionId) {
		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO
				.getHash(DIYKey.getSessionHashKey(sessionId), SystemUserSessionVO.class);
		Shop shop = shopDAO.query(systemUserSessionVO.getSystemId());
		if (shop == null) {
			throw new ShopNotFoundException("店铺不存在");
		}
		MemberCardUseBO memberCardUseBO = new MemberCardUseBO();
		BeanUtils.copyProperties(vo, memberCardUseBO);
		if(StringUtils.isNotBlank(shop.getChainId())){
			memberCardUseBO.setCsId(shop.getChainId());
		}else{
			memberCardUseBO.setCsId(shop.getId());
		}
		List<MemberCardUseBO> list = memberCardUseDAO.findAll(memberCardUseBO, dtp.getStart(), dtp.getLength());
		int totalCount = memberCardUseDAO.getTotalCount(memberCardUseBO);
		return ViewObjectUtil.dataTables(list, dtp.getDraw(), totalCount);
	}

}
